package com.dreamwhale.machinery.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName : DragonUtils
 * @Description : redis封装工具实现类
 * @Author : DragonUncle
 * @Date :  9:45 2020/4/30
 */
@Component
public class RedisUtils<T> {
    private final RedisTemplate<String, T> redisTemplate;
    private final ValueOperations<String, T> valueOperations;

    public RedisUtils(RedisTemplate<String, T> redisTemplate) {
        this.redisTemplate = redisTemplate;

        valueOperations = redisTemplate.opsForValue();

    }

    public T read(String key) {
        return valueOperations.get(key);
    }


    public Long getTime(String key) {
        return redisTemplate.getExpire(key);
    }


    public Long getTime(String key, TimeUnit timeUnit) {
        return redisTemplate.getExpire(key, timeUnit);
    }


    public void write(String key, T value) {

        redisTemplate.opsForValue().set(key, value);
    }


    public void write(String key, T value, Long time) {
        valueOperations.set(key, value, time);
    }

    public void write(String key, T value, Long time, TimeUnit timeUnit) {
        valueOperations.set(key, value, time, timeUnit);
    }

}
